package top.kaoshanji.p1ch02c;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02c-14：MethodHandles.Lookup的基本使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 15:10
 */
public class MethodHandlesLookupExample01 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesLookupExample01.class);

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
        logger.info("...代码清单 p1ch02c-14：MethodHandles.Lookup的基本使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();

        // 构造方法
        lookup.findConstructor(String.class, MethodType.methodType(void.class, byte[].class));

        // String#substring
        lookup.findVirtual(String.class, "substring", MethodType.methodType(String.class, int.class, int.class));

        // String#format
        lookup.findStatic(String.class, "format", MethodType.methodType(String.class, String.class, Object[].class));

    }

}
